package cn.jiyun.web.tag;

import cn.jiyun.dao.domain.Role;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

@Data
public class PrivilegeTag extends TagSupport {

    private static final long serialVersionUID = -532517444654109642L;

    private String operationId; // 对应Attribute,加上set方法。
    private String name;      // 按钮名（添加）
    private String clazz;     // 样式
    private String id;        // id名
    private String color;      //文字颜色

    /**
     * 解析标签，形成原有的a标签
     * <button id="btn_add" type="button" class="btn btn-default" style="color: #093F4D">
     * <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
     * </button>
     */
    public int doStartTag() throws JspException {
        Role currentRole = (Role) pageContext.getSession().getAttribute("currentRole");
        String operationIds = currentRole.getOperationIds();
        if (StringUtils.isNotEmpty(operationIds) && operationIds.contains(operationId)) {
            StringBuffer sb = new StringBuffer();
            sb.append("<button type=\"button\" ");
            sb.append("id=\"" + id + "\"");
            sb.append("class=\"btn btn-default\" style=\"color:" + color + "\">");
            sb.append("<span class=\"" + clazz + "\" aria-hidden=\"true\"></span>" + name);
            sb.append("</button>");
            try {
                pageContext.getOut().write(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return EVAL_PAGE;
        }
        return SKIP_BODY; // 跳过body,body部分不会显示
        /* 设置默认值 */
    }

}